package dy.spec.edu.week14_home01;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText editext1;
    private EditText editext2;
    private EditText editext3;
    private String stuNumber;
    private String passWord;
    private String repassWord;
    private String text;
    private int n = 0;
    LoginActivity a=new LoginActivity();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.StuButton2);
        editext1 = (EditText) findViewById(R.id.E3);
        editext2 = (EditText) findViewById(R.id.E4);
        editext3 = (EditText) findViewById(R.id.E5);
        Intent i = getIntent();
        text = i.getStringExtra ( "text" );
// Now set this value to EditText
        button.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.StuButton2:
                stuNumber = editext1.getText().toString();
                passWord = editext2.getText().toString();
                repassWord = editext3.getText().toString();
                sendkhttp();
                break;
            default:
                break;
        }
    }

    private void sendkhttp() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                // 创建Httpclient对象
                HttpClient httpClient = new DefaultHttpClient();

                // 创建http POST请求
                HttpPost httpPost = new HttpPost("http://www.yyjsx.xyz/api/student/updatePassword");

                // 设置2个post参数，一个是scope、一个是q
                List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
                parameters.add(new BasicNameValuePair("studentNumber", stuNumber));
                parameters.add(new BasicNameValuePair("password", passWord));
                // 构造一个form表单式的实体
                UrlEncodedFormEntity formEntity = null;
                if (passWord.equals(repassWord)) {
                    try {
                        formEntity = new UrlEncodedFormEntity(parameters);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    // 将请求实体设置到httpPost对象中
                    httpPost.setEntity(formEntity);
                    try {
                        // 执行请求
                        HttpResponse response = httpClient.execute(httpPost);
                        // 判断返回状态是否为200

                        if (response.getStatusLine().getStatusCode() == 200) {
                            String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                            System.out.println(content);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MainActivity.this, "修改成功", Toast.LENGTH_LONG).show();
                                    Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                                    startActivity(intent);
                                }
                            });
                        }
                    } catch (ClientProtocolException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this, "修改失败", Toast.LENGTH_LONG).show();
                            editext1.setText("");
                            editext2.setText("");
                            editext3.setText("");
                        }
                    });

                }
            }
        }).start();
    }

}
